﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace JerrodSoft.Serialization.Xml.Attributes.Annotations
{
    [AttributeUsage(AttributeTargets.Property)]
    public class EnumerationAttribute : System.ComponentModel.DataAnnotations.ValidationAttribute
    {
        public bool IsCaseSensitive { get; set; }

        public EnumerationAttribute(params string[] validValues)
        {
            if (validValues == null)
                throw new ArgumentException("An EnumerationAttribute must have at least one valid value.");

            foreach (var str in validValues)
                if (str == null)
                    throw new ArgumentException("A null value is not acceptable as a valid value for EnumerationAttribute.");

            this.ValidValues = validValues;
        }
        public readonly IEnumerable<string> ValidValues;

        public override bool IsValid(object value)
        {
            if (!(value is string))
                throw new ArgumentException("Invalid type specified for EnumerationAttribute.");

            foreach (var v in ValidValues)
                if (string.Compare(v, value.ToString(), !IsCaseSensitive) != 0)
                    return true;
            return false;
        }
    }
}
